package cn.demoxy.controller;

import cn.demoxy.annotation.Log;
import cn.demoxy.enums.BusinessTypeEnum;
import cn.demoxy.result.R;
import cn.demoxy.service.IDemoFileService;
import cn.demoxy.model.dto.DemoFileDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import cn.demoxy.model.entity.DemoFile;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 文件管理表控制层
 *
 * @author demo
 * @date 2022-05-26
 */
@Api(tags = " 文件管理表接口信息 ")
@AllArgsConstructor
@RestController
@RequestMapping("/admin/demo-file")
@CrossOrigin
public class DemoFileController {

    /**
     * 文件service
     */
    private IDemoFileService demoFileService;


    /**
     * 查询所有
     *
     * @return
     * @params
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有", notes = "查询所有")
    @RequiresPermissions("demo-file:list")
    public R list() {
        return R.ok(this.demoFileService.list());
    }

    /**
     * 分页查询
     *
     * @return
     * @params dto
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @RequiresPermissions("demo-file:page")
    public R page(@RequestBody DemoFileDTO dto) {
        return R.ok(this.demoFileService.page(dto.page()));
    }

    /**
     * 通过id查询
     *
     * @return
     * @params id
     */
    @GetMapping("/get")
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @RequiresPermissions("demo-file:get")
    public R get(String id) {
        return R.ok(this.demoFileService.getById(id));
    }

    /**
     * 添加
     *
     * @return
     * @params entity
     */
    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping("/save")
    @RequiresPermissions("demo-file:save")
    public R save(@RequestBody DemoFile entity) {
        return R.ok(this.demoFileService.save(entity));
    }

    /**
     * 更新
     *
     * @return
     * @params entity
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新", notes = "更新")
     @RequiresPermissions("demo-file:update")
    public R update(@RequestBody DemoFile entity) {
        return R.ok(this.demoFileService.updateById(entity));
    }

    /**
     * 通过id删除
     *
     * @return
     * @params id
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "删除文件", notes = "删除文件")
    @Log(title = "文件管理文件删除",businessType = BusinessTypeEnum.DELETE)
    @RequiresPermissions("demo-file:delete")
    public R delete(@PathVariable("id") String id) {
        return R.ok(this.demoFileService.removeFile(id));
    }

    /**
     * minio文件上传
     *
     * @param files
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadFiles")
    @ApiOperation(value = "文件上传", notes = "文件上传")
    @Log(title = "文件管理文件上传",businessType = BusinessTypeEnum.UPLOAD)
    @RequiresPermissions("demo-file:upload")
    public R uploadFiles(MultipartFile[] files) {
        demoFileService.uploadFiles(files);
        return R.ok();
    }

    /**
     * minio文件下载
     *
     * @param fileName
     * @param response
     * @throws IOException
     */
    @GetMapping("/downloadFile")
    @ApiOperation(value = "文件下载", notes = "文件下载")
    @Log(title = "文件管理文件下载",businessType = BusinessTypeEnum.DOWNLOAD)
    @RequiresPermissions("demo-file:download")
    public void downloadFile(String fileName, HttpServletResponse response) {
        demoFileService.downloadFile(fileName, response);
    }

    /**
     * 文件预览
     * @param fileName
     * @return
     */
    @GetMapping("/previewFile")
    @ApiOperation(value = "文件预览", notes = "文件预览")
    @Log(title = "文件管理文件预览",businessType = BusinessTypeEnum.PREVIEW)
    @RequiresPermissions("demo-file:preview")
    public R previewFile(String fileName) {
        String fileUrl = demoFileService.previewFile(fileName);
        return R.ok(fileUrl);
    }

    /**
     * minio文件上传
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadFace")
    @ApiOperation(value = "头像上传", notes = "头像上传")
    @Log(title = "文件管理头像上传",businessType = BusinessTypeEnum.UPLOAD)
//    @RequiresPermissions("demo-file:upload")
    public R uploadFace(MultipartFile file) {
        return R.ok(demoFileService.uploadFace(file));
    }
}
